//
//  ZLHomeVc.swift
//  swiftDemo
//
//  Created by 众力金融 on 2018/8/28.
//  Copyright © 2018年 众力金融. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyJSON
import HandyJSON

struct constants {
    static let homeCellID = "homeCellID"
}

class ZLHomeVc: ZLBaseViewController {
    
    fileprivate lazy  var tableView = UITableView()
    var homeModel = BaseDataMode();

    override func viewDidLoad() {
        super.viewDidLoad()
      view.backgroundColor = UIColor.white;
      setupTableView()
        loadData()
    }

}


extension ZLHomeVc  {
   fileprivate func setupTableView() {
    view.addSubview(tableView)
    tableView.snp.makeConstraints { (make) in
        make.top.left.right.bottom.equalTo(0)
    }
    tableView.delegate = self
    tableView.dataSource = self
    tableView.rowHeight = 230
//    let nib = UINib(nibName: "QXHomeTopicCell", bundle: nil)
//    tableView.register(nib, forCellReuseIdentifier: homeCellID)
    tableView.register(ZLHomeCell.self, forCellReuseIdentifier: constants.homeCellID)
    //设置分割线
    //tableView.separatorStyle = .none
   tableView.tableFooterView = UIView()
    }
    
    func loadData() {
        let url = "http://api.qxsoho.com" + "/app/home/getHomeIndex"
        
        NetWorkTool.requestData(type: .post, urlString:url, parameters: nil, succeed: { (result) in
            
            print(result)
            if let modeldata = BaseDataMode.deserialize(from: result as? NSDictionary) {
                self.homeModel = modeldata
            }
             self.tableView.reloadData()

        }) { (error) in

        }
    }
}

//MARK: - UITableViewDataSource, UITableViewDelegate 添加注释
extension ZLHomeVc : UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return homeModel.recommendProductList!.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ZLHomeCell(style: UITableViewCellStyle.default, reuseIdentifier: constants.homeCellID)
        cell.backgroundColor = UIColor.white
        cell.selectionStyle = .none
        cell.homeCellModel = homeModel.recommendProductList![indexPath.row]
        cell.btnCLickBolock = { (title) in
           print(1111)
            print(title)
        }
        return cell
        
    }

}
